﻿using System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using LEDDataBase;
using LEDHelper;
using System.Windows.Interop;
using System.Windows.Shapes;
namespace LEDManager
{
    public partial class LEDUnit
    {
        private void LED_FastenProgram()
        {
            try
            {
                m_data.Reset();
                #region=============
                DataRow dwContent;
                FontDescriptor lpFont;
                int method, speed, transparent;
                string fontID;
                dwProg = null;
                int nScreenType = LEDAPI.SCREEN_COLOR;
                var query = from dwPr in m_data.LEDDatas.PROGRAM.AsEnumerable()
                            where dwPr["PRPROGRAMID"].ToString() == progID
                            select dwPr;
                foreach (DataRow dwTemp in query)
                {
                    dwProg = dwTemp;
                }
                if (dwProg == null)
                    return;
                #endregion
                string filter = "LFSYSTEM='" + system + "' and LFLEDID='" + ledid + "' and LFPROGRAMID='" + progID + "'";
                DataRow[] rowsl = m_data.LEDDatas.LEAF.Select(filter);
                //MessageBox.Show(filter + rowsl.Length);
                DataRow[] rowsd;
                if (rowsl.Length == 0)
                    return;
                nScreenType = System.Convert.ToInt32(dwProg["PRSCREENTYPE"].ToString());
                PerformCommand("LED_Open", nScreenType, nWidth, nHeight, LEDAPI.ROOT_DOWNLOAD);
                string processlefid = "";
                foreach (DataRow dwLf in rowsl)
                {

                    string lefid = dwLf["LFLEAFID"].ToString();
                    if (processlefid.Contains(lefid + ";"))
                        continue;
                    processlefid += lefid + ";";
                    int lDisplayTime = System.Convert.ToInt32(dwLf["LFDURATION"].ToString());
                    filter = "LDLEAFID='" + lefid + "'";
                    PerformCommand("AddLeaf", lDisplayTime, "");
                    rowsd = m_data.LEDDatas.LEAFDETAIL.Select(filter);
                    foreach (DataRow dwLd in rowsd)
                    {
                        string ctype = dwLd["LDCONTENTTYPE"].ToString();
                        string cid = dwLd["LDCONTENTID"].ToString();
                        #region=====LEDBlock========
                        int LDLTOPX = System.Convert.ToInt32(dwLd["LDLTOPX"].ToString());
                        int LDLTOPY = System.Convert.ToInt32(dwLd["LDLTOPY"].ToString());
                        int LDLBOTTOMX = System.Convert.ToInt32(dwLd["LDLBOTTOMX"].ToString());
                        int LDLBOTTOMY = System.Convert.ToInt32(dwLd["LDLBOTTOMY"].ToString());
                        int LDRTOPX = System.Convert.ToInt32(dwLd["LDRTOPX"].ToString());
                        int LDRTOPY = System.Convert.ToInt32(dwLd["LDRTOPY"].ToString());
                        int LDRBOTTOMX = System.Convert.ToInt32(dwLd["LDRBOTTOMX"].ToString());
                        int LDRBOTTOMY = System.Convert.ToInt32(dwLd["LDRBOTTOMY"].ToString());

                        LEDBlock rect = new LEDBlock();
                        rect.nStartX = LDLTOPX;
                        rect.nStartY = LDLTOPY;
                        rect.nWidth = LDRTOPX - LDLTOPX;
                        rect.nHeight = LDLBOTTOMY - LDLTOPY;
                        #endregion
                        switch (ctype)
                        {
                            case "CTEXT":
                                dwContent = m_data.GetContent("CTEXT", cid);
                                string szText = dwContent["CTCONTENT"].ToString();
                                method = System.Convert.ToInt32(dwContent["EFFECT"].ToString());
                                speed = System.Convert.ToInt32(dwContent["SPEED"].ToString());
                                transparent = System.Convert.ToInt32(dwContent["TRANSPARENT"].ToString());
                                fontID = dwContent["FONTID"].ToString();
                                dwFont = m_data.GetContent("FONTTABLE", fontID);
                                lpFont = new FontDescriptor();
                                lpFont.szFontName = dwFont["CFFONTNAME"].ToString();
                                lpFont.lFontSize = System.Convert.ToInt32(dwFont["CFFONTSIZE"].ToString());
                                lpFont.lFontColor = System.Convert.ToInt32(dwFont["CFCOLOR"].ToString());
                                PerformCommand("AddText", szText, rect, lpFont, method, speed, transparent);
                                break;
                            case "CPICTURE":
                                dwContent = m_data.GetContent("CPICTURE", cid);
                                string szBmpFile = dwContent["CPPICTUREFILE"].ToString();
                                method = System.Convert.ToInt32(dwContent["EFFECT"].ToString());
                                speed = System.Convert.ToInt32(dwContent["SPEED"].ToString());
                                transparent = System.Convert.ToInt32(dwContent["TRANSPARENT"].ToString());
                                LEDBlock destRect = new LEDBlock();
                                LEDBlock sourceRect = new LEDBlock();
                                sourceRect.nStartX = 0;
                                sourceRect.nStartY = 0;
                                sourceRect.nWidth = System.Convert.ToInt32(dwContent["CPWIDTH"].ToString());
                                sourceRect.nHeight = System.Convert.ToInt32(dwContent["CPHEIGHT"].ToString());
                                destRect.nStartX = rect.nStartX;
                                destRect.nStartY = rect.nStartY;
                                destRect.nWidth = rect.nWidth;
                                destRect.nHeight = rect.nHeight;
                                PerformCommand("AddWindow", szBmpFile, destRect, sourceRect, method, speed, transparent);
                                break;
                            case "CCLOCK":
                                dwContent = m_data.GetContent("CCLOCK", cid);
                                ClockDescriptor lpClock = new ClockDescriptor();
                                fontID = dwContent["FONTID"].ToString();
                                dwFont = m_data.GetContent("FONTTABLE", fontID);
                                lpFont = new FontDescriptor();
                                lpFont.szFontName = dwFont["CFFONTNAME"].ToString();
                                lpFont.lFontSize = System.Convert.ToInt32(dwFont["CFFONTSIZE"].ToString());
                                lpFont.lFontColor = System.Convert.ToInt32(dwFont["CFCOLOR"].ToString());
                                method = System.Convert.ToInt32(dwContent["EFFECT"].ToString());
                                speed = System.Convert.ToInt32(dwContent["SPEED"].ToString());
                                transparent = System.Convert.ToInt32(dwContent["TRANSPARENT"].ToString());
                                PerformCommand("AddDateTime", rect, lpClock, lpFont, method, speed, transparent);
                                break;
                            default:
                                break;
                        }
                    }
                }
                PerformCommand("LED_EndProgram");
                PerformCommand("LED_SendToScreen");
                //初始化一个节目表
                //在编辑过程中添加，建立节目
                //
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void LED_EndProgram()
        {
            try
            {
                m_data.Save();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ConverBmpToFixedProgram(string szBmpFile)
        {
            try
            {
                int nScreenType = LEDAPI.SCREEN_COLOR;
                PerformCommand("LED_Open", nScreenType, nWidth, nHeight, LEDAPI.ROOT_DOWNLOAD);
                PerformCommand("AddLeaf", 10000, "");
                int method = 1;
                int speed = 1;
                int transparent = 1;
                LEDBlock destRect = new LEDBlock();
                LEDBlock sourceRect = new LEDBlock();
                sourceRect.nStartX = 0;
                sourceRect.nStartY = 0;
                sourceRect.nWidth = nWidth;
                sourceRect.nHeight = nHeight;
                destRect.nStartX = 0;
                destRect.nStartY = 0;
                destRect.nWidth = nWidth;
                destRect.nHeight = nHeight;
                PerformCommand("AddWindow", szBmpFile, destRect, sourceRect, method, speed, transparent);
                PerformCommand("LED_EndProgram");
                PerformCommand("LED_SendToScreen");
                PerformCommand("LED_FastenProgram"); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public Int32 PerformCommand(string command, object p1 = null, object p2 = null, object p3 = null, object p4 = null, object p5 = null
            , object p6 = null, object p7 = null, object p8 = null)
        {
            Int32 result = 0;
            string szBmpFile;
            int nScreenType = LEDAPI.SCREEN_COLOR;
            Int32 nSpeed = 1;
            Int32 lTrasnparent = 1;
            Int32 nEffectType = 1;
            Int32 method = nEffectType;
            Int32 speed = nSpeed;
            Int32 transparent = lTrasnparent;
            int playtype = LEDAPI.ROOT_PLAY;
            LEDBlock rect = new LEDBlock();
            SRECT rectx = new SRECT();
            FontDescriptor lpFont = new FontDescriptor();
            LEDBlock destRect = new LEDBlock();
            LEDBlock sourceRect = new LEDBlock();
            int iDisplaytime = 100;
            try
            {
                switch (command)
                {
                    case "LED_Open":
                        nScreenType = (int)p1;
                        nWidth = System.Convert.ToInt32(p2);
                        nHeight = System.Convert.ToInt32(p3);
                        playtype = (int)p4;
                        MakeRoot(playtype, screentyp);
                        dwProg = m_data.AddPrograme(system, ledid, playtype, nScreenType);
                        progID = dwProg["PRPROGRAMID"].ToString();
                        break;
                    case "LED_Query":
                        LED_Query(dev, devid, Host, port);
                        break;
                    case "LED_SetPower":
                        Int32 power = (Int32)p1;
                        dwLEDStatus["STPOWER"] = power;
                        LED_SetPower(dev, devid, Host, port, power);
                        break;
                    case "LED_GetPower":
                        LED_GetPower(dev, devid, Host, port);
                        break;
                    case "LED_SetBrightness":
                        Int32 bright = (Int32)p1;
                        dwLEDStatus["STBRIGHT"] = bright;
                        LED_SetBrightness(dev, devid, Host, port, bright);
                        break;
                    case "LED_GetBrightness":
                        LED_GetBrightness(dev, devid, Host, port);
                        break;
                    case "LED_AdjustTime":
                        LED_AdjustTime(dev, devid, Host, port);
                        break;
                    case "LED_SendToScreen":
                        LED_SendToScreen(dev, devid, Host, port);
                        break;
                    case "LED_PreUploadBMP":
                        szBmpFile = (string)p1;
                        BackGroundBmp = szBmpFile;
                        dwLEDStatus["STBGBMP"] = BackGroundBmp;
                        DumpStatusData();
                        ConverBmpToFixedProgram(szBmpFile);
                        break;
                    case "LED_RemoveUploadedBmp":
                        szBmpFile = (string)p1;
                        BackGroundBmp = szBmpFile;
                        dwLEDStatus["STBGBMP"] = "NO";
                        DumpStatusData();
                        break;
                    case "LED_GetFreeUploadBmpSpace":
                        LEDData m_Data = new LEDData();
                        m_data.ResetLedData();
                        break;
                    case "LED_FastenProgram":
                        LED_FastenProgram();
                        break;
                    case "LED_EndProgram":
                        LED_EndProgram();
                        break;
                    case "AddLeaf":
                        iDisplaytime = (int)p1;
                        AddLeaf(iDisplaytime);
                        dwLeaf = m_data.AddLeaf(dwProg, iDisplaytime);
                        BackGroundBmp = (string)p2;
                        if (System.IO.File.Exists(BackGroundBmp))
                        {
                            method = 1;
                            speed = 1;
                            transparent = 1;
                            destRect = new LEDBlock();
                            sourceRect = new LEDBlock();
                            sourceRect.nStartX = 0;
                            sourceRect.nStartY = 0;
                            sourceRect.nWidth = nWidth;
                            sourceRect.nHeight = nHeight;
                            destRect.nStartX = 0;
                            destRect.nStartY = 0;
                            destRect.nWidth = nWidth;
                            destRect.nHeight = nHeight;
                            PerformCommand("AddWindow", BackGroundBmp, destRect, sourceRect, method, speed, transparent);
                        }
                        break;
                    case "AddText":
                        #region=====AddText=====
                        string szText = (string)p1;
                        rect = (LEDBlock)p2;
                        lpFont = (FontDescriptor)p3;
                        method = (Int32)p4;
                        speed = (Int32)p5;
                        transparent = (Int32)p6;
                        rectx = new SRECT();
                        rectx.left = rect.nStartX;
                        rectx.top = rect.nStartY;
                        rectx.right = rect.nStartX + rect.nWidth;
                        rectx.bottom = rect.nStartY + rect.nHeight;
                        string fontname = (lpFont.szFontName).ToString();
                        Int32 fontsize = lpFont.lFontSize;
                        Int32 fontcolor = lpFont.lFontColor;
                        AddText(szText, ref rectx, method, speed, transparent, fontname, fontsize, fontcolor);
                        dwFont = m_data.AddFont(system, fontname, fontsize, fontcolor, 0, 0, 0, method);
                        dwText = m_data.AddText(system, szText, dwFont["CONTENTID"].ToString(), "", method, speed, transparent);
                        dwLeafDetail = m_data.AddLeafDetail(dwLeaf, "CTEXT", dwText["CONTENTID"].ToString(), rectx);
                        #endregion
                        break;
                    case "AddWindow":
                        #region=====AddWindow=====
                        szBmpFile = (string)p1;
                        if (!System.IO.File.Exists(szBmpFile))
                        { 
                            return -1;
                        }
                        destRect = (LEDBlock)p2;
                        //MessageBox.Show("(nStartX:" + destRect.nStartX + "nStartY:" + destRect.nStartY);
                        sourceRect = (LEDBlock)p3;
                        rectx = new SRECT();
                        method = (Int32)p4;
                        speed = (Int32)p5;
                        transparent = (Int32)p6;
                        Int32 bmpwidth = sourceRect.nWidth;
                        Int32 bmpheight = sourceRect.nHeight;
                        int ledwidth = destRect.nWidth;
                        int ledheight = destRect.nHeight;
                        int nStartX=destRect.nStartX;
                        int nStartY=destRect.nStartY;
                        SetRect(ref rectx, nStartX, nStartY, ledwidth, ledheight);
                        IntPtr screenDC = GetDC(IntPtr.Zero);
                        IntPtr memDC = CreateCompatibleDC(screenDC);
                        Bitmap bmp = new Bitmap(szBmpFile);
                        IntPtr hBitmap = bmp.GetHbitmap();
                        hBitmap = bmp.GetHbitmap();
                        SelectObject(memDC, hBitmap); // Select bitmap from compatible bitmap to memDC 
                        AddWindow(memDC, bmpwidth, bmpheight, ref rectx, method, speed, lTrasnparent);
                        DeleteObject(hBitmap);
                        ReleaseDC(IntPtr.Zero, screenDC);
                        ReleaseDC(IntPtr.Zero, memDC);

                        dwPicture = m_data.AddPicture(system, szBmpFile, bmpwidth, bmpheight, method, speed, transparent);
                        dwLeafDetail = m_data.AddLeafDetail(dwLeaf, "CPICTURE", dwPicture["CONTENTID"].ToString(), rectx);
                        #endregion
                        break;
                    case "AddDateTime":
                        #region=====AddDateTime=====
                        rect = (LEDBlock)p1;
                        ClockDescriptor lpClock = (ClockDescriptor)p2;
                        lpFont = (FontDescriptor)p3;
                        method = (Int32)p4;
                        speed = (Int32)p5;
                        transparent = (Int32)p6;
                        rectx = new SRECT();
                        rectx.left = rect.nStartX;
                        rectx.top = rect.nStartY;
                        rectx.right = rect.nStartX + rect.nWidth;
                        rectx.bottom = rect.nStartY + rect.nHeight;
                        fontname = "楷书";
                        //string fontname = lpFont.szFontName;
                        fontsize = lpFont.lFontSize;
                        fontcolor = lpFont.lFontColor;
                        Int32 format = 1;//DF_YMD
                        /*
                         * 1 DF_YMD:	 YYYY年MM月DD日 (2004年01月01日)
                         * 2 DF_HN:      HH:NN     (19:20)
                         * 3 DF_HNS:     HH:NN:SS  (19:20:30)
                         * 4 DF_Y:       YYYY  (2004)
                         * 5 DF_M:       MM    (01)
                         * 6 DF_D:       DD     (01)
                         * 7 DF_H:       HH     (19)
                         * 8 DF_N:       NN     (20)
                         * 9 DF_S:       SS      (30)
                         * 10 DF_W:      星期    (星期三)*/
                        AddDateTime(ref rectx, transparent, fontname, fontsize, fontcolor, format);
                        dwFont = m_data.AddFont(system, fontname, fontsize, fontcolor, 0, 0, 0, method);
                        dwClock = m_data.AddClock(system, dwFont["CONTENTID"].ToString(), "DIGITAL", method, speed, transparent);
                        dwLeafDetail = m_data.AddLeafDetail(dwLeaf, "CCLOCK", dwClock["CONTENTID"].ToString(), rectx);
                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                result = 0;
            }
            return result;
        }
    }
}
